package com.test.ssh.dao;

import com.test.ssh.entities.Employee;
import org.hibernate.Query;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author liuziyang
 * @create 2018-10-07 21:44
 */
public class EmployeeDao extends BaseDao {

    public List<Employee> getAll() {
//        String hql = "FROM Employee";
        String hql = "FROM Employee e LEFT OUTER JOIN FETCH e.department";
        return getSession().createQuery(hql).list();
    }

    public void delete(Integer id) {
        String hql = "DELETE FROM Employee e WHERE e.id=?";
        getSession().createQuery(hql).setInteger(0, id).executeUpdate();
    }

    public void saveOrUpdate(Employee employee) {
        getSession().saveOrUpdate(employee);
    }

    public Employee getEmployeeByLastName(String lastName) {
        String hql = "FROM Employee e WHERE e.lastName=?";
        Query query = getSession().createQuery(hql).setString(0, lastName);
        return (Employee) query.uniqueResult();
    }

    public Employee getEmployeeById(Integer id) {
        String hql = "FROM Employee e LEFT OUTER JOIN FETCH e.department WHERE e.id=?";
        return (Employee) getSession().createQuery(hql).setInteger(0, id).uniqueResult();
    }
}
